package StudyCode.summary.mycollection;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class RemoveItemFromArray {
	public static void main(String[] argv) {
		String object = "www.tah1986.com";
		String[] array = new String[]{"1", "abc", "level", null,
				"www.tah1986.com", "asdf 123"};
		System.out.println("删除 www.tah1986.com 后:");
		System.out.println(java.util.Arrays.toString(removeFromList(object, array)));
	}

	public static String[] removeFromList(String object, String[] array) {
		String[] ret = null;
		int remIndex = -1;
		try {
			for (int i = 0; i < array.length; i++) {
				if (object.equals(array[i])) {
					remIndex = i;
					break;
				}
			}
			if (remIndex != -1) {
				ret = new String[array.length - 1];
				int ri = 0;
				int wi = 0;
				while (wi < ret.length) {
					if (ri == remIndex) {
						ri++;
					} else {
						ret[wi] = array[ri];
						ri++;
						wi++;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Objects aren't same type");
		}
		return ret;
	}
}
